package com.photoeditor.demo.ui.widget.image.frame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.photoeditor.R;
import com.photoeditor.demo.util.ImageRectUtils;


/**
 * @dest 相框列表底部背景view
 */

public class PhotoFrameBaseBgView extends RelativeLayout {

    private Paint mBorderPaint;
    private float mDefaultBorderWidth;

    private boolean mIsInit = false;

    //相对于屏幕
    private RectF mViewRect;

    private boolean mShow = false;

    public PhotoFrameBaseBgView(Context context) {
        super(context);
    }

    public PhotoFrameBaseBgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        mDefaultBorderWidth = getResources().getDimensionPixelSize(R.dimen.animation_textview_border_width);
        mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBorderPaint.setColor(getResources().getColor(R.color.accent_color));
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(mDefaultBorderWidth);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            RectF rectF = ImageRectUtils.getViewRect(this);
            if ((!mIsInit || !rectF.equals(mViewRect))) {
                mViewRect = rectF;
                mIsInit = true;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mIsInit) {
            if (mShow) {
                canvas.drawRect(0, 0, mViewRect.width(), mViewRect.height(), mBorderPaint);
            }
        }
    }

    /**
     * 设置颜色
     *
     * @param color
     */
    public void setColor(int color) {
        mBorderPaint.setColor(color);
        postInvalidate();
    }

    /**
     * 设置显示边界或者不显示
     *
     * @param show
     */
    public void setShow(boolean show) {
        mShow = show;
        postInvalidate();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

    }
}
